/* postcss-simple-vars */
$plus-minus-pad: .5rem;

:root {
  --pad: 1rem; /* Edge padding for modals/blocks/whatnot. TODO: reuse it in more places */
  --pad05: calc(0.5 * var(--pad));
  --popup-button-width: 16px;
  --target-gapY: 2px;
}

#global-progress {
  position: fixed;
  height: 4px;
  top: 0;
  left: 0;
  right: 0;
  background-color: hsla(180, 66%, 36%, .25);
  border-left: 0 solid darkcyan;
  z-index: 2147483647;
  transition: opacity 2s;
  contain: strict;
  &:not([title]) {
    opacity: 0;
  }
}

.is-new-style .hide-if-new,
.hidden {
  display: none !important;
}
html.is-new-style #heading::before {
  content: attr(data-add);
}
html:not(.is-new-style) #heading::before {
  content: attr(data-edit);
}

/************ embedded popup for simple-window editor ************/
#popup-iframe {
  max-height: 600px;
  position: fixed;
  right: 0;
  top: 0;
  z-index: 1001;
  border: none;
  background: var(--bg);
  box-shadow: 0 0 30px var(--fg);
  &:not([data-loaded]) {
    opacity: 0;
  }
}
#popup-button {
  position: fixed;
  right: 7px;
  top: 11px;
  z-index: 1000;
  cursor: pointer;
  transition: filter .25s;
  &:hover {
    filter: drop-shadow(0 0 3px hsl(180, 70%, 50%));
  }
  .usercss:not(.compact-layout) & {
    right: 24px;
  }
}

/************ header ************/
#header {
  width: var(--header-width);
  overflow: hidden;
  position: fixed;
  top: 0;
  padding-top: var(--pad);
  z-index: 10;
  display: flex;
  flex-direction: column;
  h1 {
    margin-top: 0;
    .usercss & {
      margin-bottom: 0;
      + a {
        margin-bottom: 1em;
      }
    }
  }
}
.sectioned {
  > body {
    background-color: var(--c98);
  }
  #sections {
    counter-reset: codebox;
  }
}
#sections {
  padding-left: var(--header-width);
  .usercss & {
    min-height: 0;
    height: 100%;
  }
  h2 {
    margin-top: 1rem;
    margin-left: 1.7rem;
  }
}

/* basic info */
#basic-info {
  margin-bottom: 1rem;
}
#name {
  width: 100%;
}
#basic-info-name {
  display: flex;
  align-items: center;
}
#reset-name {
  margin: 0 .25em 0 .5em;
}
#url {
  margin-left: 0.25rem;
}
#url:not([href^="http"]) {
  display: none;
}

#basic-info-enabled {
  margin-top: .25rem;
  > :nth-last-child(n + 2) {
    margin-right: var(--pad05);
  }
}
#livePreview-settings {
  display: inline-flex;
  align-items: center;
  .compact-layout & {
    margin-right: 3ex;
  }
  summary {
    padding-left: .4rem;
    display: block;
  }
  details[open] summary i {
    color: var(--fg);
  }
  main {
    position: absolute;
    z-index: 100;
    right: 2em;
    box-shadow: 2px 2px .5em var(--c65);
    margin-top: .5em;
    padding: 1em;
    background: var(--bg);
    display: flex;
    align-items: center;
    > input {
      field-sizing: content;
      padding-left: .5em;
      margin-right: 1ch;
      max-width: 8ch;
    }
  }
}
#preview-errors:not([hidden]) {
  color: var(--red1);
  cursor: pointer;
  display: inline-flex;
  margin: var(--pad05) var(--pad05) -1em 0;
  max-width: 100%;
  .compact-layout & {
    margin-top: 0;
    max-width: 32vw;
  }
  .i-external {
    font-size: 15px;
  }
}

i {
  width: 16px;
  height: 16px;
}
a.icon {
  margin-left: .2rem;
}
.i-info {
  width: 14px;
  height: 16px;
}
i:hover,
.i-info,
.i-config {
  color: var(--c40);
}
i,
.i-info:hover,
.i-config:hover {
  color: var(--fg);
}
/* collapsibles */
#details-wrapper {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  overflow: hidden;
  margin-top: var(--pad05);
  details {
    margin-right: var(--header-resizer-width);
    &[open] {
      overflow-y: auto;
      margin-top: calc(1.5 * var(--pad));
      .normal-layout &.settings {
        min-height: 6em;
      }
      > summary {
        position: absolute;
        margin-top: calc(-1.5 * var(--pad));
      }
    }
  }
  summary {
    align-items: center;
    margin-left: .25em;
    white-space: nowrap;
    overflow: hidden;
  }
  h2 {
    display: inline-block;
    border-bottom: 1px dotted transparent;
    margin: 0 0 0 -13px;
    padding-left: 13px; /* clicking directly on details-marker doesn't set pref so we cover it with h2 */
    max-width: calc(var(--header-width) - 2*var(--pad));
    vertical-align: middle;
    font-size: 16px;
    &,
    > :first-child {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
  .icon {
    margin-top: -.1em;
  }
  details > :not(summary) {
    margin: 0 var(--pad05) 0 var(--pad);
    padding: calc(var(--pad)/4) 0;
  }
}
#actions .buttons {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  > * {
    margin-bottom: var(--pad05);
    margin-right: .25rem;
  }
}
.dirty #save-button {
  &, & + button {
    background: linear-gradient(gold, orange);
    border-color: darkgoldenrod;
    color: #000;
  }
}
#publish {
  > div > * {
    margin-top: .75em;
  }
  a:visited {
    margin-top: .75em;
  }
  &[data-connected] {
    > summary::marker,
    h2 {
      color: hsl(180, 100%, 20%);
    }
    #usw-publish-style::after {
      content: attr(data-push);
    }
  }
  &:not([data-connected]) {
    #usw-disconnect {
      display: none;
    }
    #usw-publish-style::after {
      content: attr(data-publish);
    }
  }
}
#usw-progress {
  position: relative;
  :is(.success, .unchanged) {
    font-size: 150%;
    font-weight: bold;
    position: absolute;
    margin-left: .25em;
  }
  .success {
    margin-top: -.25em;
    &::after {
      content: '\2713'; /* checkmark */
    }
  }
  .unchanged::after {
    content: '=';
  }
  .error {
    display: block;
    margin-top: .5em;
    color: red;
    + div {
      font-size: smaller;
    }
  }
  .lds-spinner {
    transform: scale(0.125);
    transform-origin: 0 calc(var(--input-height) / -3);
  }
}
#usw-data {
  > * > * {
    margin-top: 0;
    vertical-align: baseline;
    --input-height: 1rem;
  }
  .empty label {
    color: var(--c60);
  }
  input {
    width: 100%;
    &:not(:focus):not(:hover) {
      background: none;
      border-color: transparent;
    }
  }
}
/* footer */
.usercss #footer {
  display: block;
  margin-top: 1em;
  margin-bottom: .5em;
}
#lint:not([open]) + #footer {
  margin-top: 4em;
}
/************ section editor ***********/
.CodeMirror-vscrollbar,
.CodeMirror-hscrollbar {
  box-shadow: none !important;
  pointer-events: auto !important; /* FF bug */
}
.section {
  --padL: calc(var(--pad) * 1.5);
  padding: var(--pad) 0; /* must be a part of section for fitToAvailableSpace */
  > .CodeMirror {
    content-visibility: auto;
    contain-intrinsic-block-size: auto var(--cm-height);
  }
  > .applies-to li,
  > .edit-actions {
    content-visibility: auto;
    contain-intrinsic-block-size: auto var(--input-height);
  }
  .sectioned:not(.compact-layout) & {
    > * {
      margin-right: var(--pad);
    }
    > :not(.applies-to) {
      margin-left: var(--padL);
    }
  }
  &:nth-child(n + 2) {
    border-top: 1px dotted var(--c80);
  }
}
.add-section::after {
  content: attr(short-text);
}
#sections[data-section-count="1"] {
  .add-section:after {
    content: attr(long-text);
  }
  .remove-section {
    display: none;
  }
}
.edit-actions {
  display: flex;
  flex-wrap: wrap;
  button {
    margin-right: .2rem;
    height: var(--input-height);
  }
}
.section:not(.removed) > label {
  display: block;
  font-weight: bold;
  animation: 2s highlight;
  animation-play-state: paused;
  animation-direction: reverse;
  animation-fill-mode: both;
}
.usercss .applies-to {
  border-width: 1px 0;
  > label::before {
    content: attr(data-index) ":";
    margin-right: 0.25rem;
    font-size: var(--font-size);
  }
}
.code-label {
  counter-increment: codebox;
  > :first-child::after {
    content: " " counter(codebox) ": ";
  }
  &.warn {
    color: red;
    font-size: 120%;
    i {
      color: red;
      margin-left: .25em;
    }
  }
}
#toc-title[data-num]::after {
  content: " (" attr(data-num) ")"
}
#toc {
  counter-reset: codelabel;
  li {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    &.current {
      font-weight: bold;
    }
    &[tabindex="-1"] {
      opacity: .25;
      pointer-events: none;
    }
    &:hover {
      background-color: hsla(180, 50%, 36%, .2);
    }
    &[tabindex="0"]::before {
      counter-increment: codelabel;
      content: counter(codelabel) ": ";
    }
  }
}
.section {
  &:only-of-type :is(.move-section-up, .move-section-down) {
    display: none;
  }
  .CodeMirror {
    margin-bottom: calc(1px + var(--target-gapY));
    box-sizing: border-box;
    min-height: 20px;
    background-color: var(--bg);
  }
  /* deleted section */
  &.removed {
    &:first-child {
      padding-top: var(--pad);
    }
    :is(.code-label, .applies-to, .edit-actions, .CodeMirror) {
      display: none;
    }
  }
}
.deleted-section {
  margin: 0;
}
.move-section-down:after,
.move-section-up:after {
  content: "";
  display: block;
  border: var(--side) solid transparent;
  --side: .4em;
}
.move-section-up:after {
  border-bottom: calc(var(--side) * 1.3) solid currentColor;
  margin: -50% 0 0 0;
}
.move-section-down:after {
  border-top: calc(var(--side) * 1.3) solid currentColor;
  margin: 0 0 -60% 0;
}
/* code */
.code {
  height: 10rem;
  width: 40rem;
}
.resize-grip-enabled {
  .CodeMirror-scroll {
    height: auto !important;;
    position: absolute !important;
    top: 0;
    left: 0;
    right: 0;
    bottom: 6px; /* resize-grip height */
  }
  .CodeMirror-vscrollbar {
    margin-bottom: 7px; /* make space for resize-grip */
  }
  .CodeMirror-hscrollbar {
    bottom: 7px; /* make space for resize-grip */
  }
  .CodeMirror-scrollbar-filler {
    bottom: 7px; /* make space for resize-grip */
  }
}
@keyframes highlight {
  from {
    background-color: var(--gold1);
  }
}
.resize-grip {
  position: absolute;
  display: block;
  height: 6px;
  content: "";
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9;
  cursor: n-resize;
  background-color: inherit;
  border-top-width: 1px;
  border-top-style: solid;
  border-top-color: inherit;
  &::after {
    content: "";
    bottom: 2px;
    left: 0;
    right: 0;
    margin: 0 8px;
    display: block;
    position: absolute;
    border-top-width: 2px;
    border-top-style: dotted;
    border-top-color: inherit;
  }
}
/* applies-to */
.applies-to {
  display: flex;
  align-items: flex-start;
  margin-bottom: 1px;
  &.error {
    background-color: #f002;
    border-color: #f008;
  }
  > label {
    font-size: 0;
    padding: 0;
    line-height: var(--input-height);
    height: var(--input-height);
  }
  .sectioned & label {
    width: var(--padL);
    text-align: center;
  }
  .compact-layout & label {
    display: none;
  }
  ul {
    flex: auto;
    flex-grow: 1;
    margin: 0;
    padding: 0;
    --input-height: 1.2rem;
  }
  li {
    display: flex;
    flex-wrap: wrap;
    list-style-type: none;
    align-items: center;
    padding: 1px 0; /* room for focus outline */
    margin-bottom: var(--target-gapY);
    &.applies-to-everything {
      align-items: unset;
      line-height: var(--input-height);
    }
    .add-applies-to:first-child {
      margin-left: 1rem;
    }
    .applies-value-wrapper {
      flex-grow: 1;
      display: flex;
    }
    .applies-value {
      flex-grow: 1;
      padding-left: 0.2rem;
      &:invalid {
        background: var(--bg);
        color: var(--fg);
        border-color: var(--red1);
        --focus-color1: var(--red1);
        --focus-color2: transparent;
      }
    }
  }
  input {
    border-color: var(--c85);
    caret-color: auto;
  }
  .select-wrapper {
    margin-right: calc(2px + var(--target-gapY));
  }
  &[data-all] .applies-value-wrapper {
    display: none;
  }
  img {
    vertical-align: bottom;
  }
}
.add-applies-to,
.remove-applies-to {
  font-size: 0;
  cursor: pointer;
  margin-right: $plus-minus-pad;
}
.remove-applies-to {
  margin-left: $plus-minus-pad;
}
:is(.add-applies-to, .remove-applies-to) i {
  pointer-events: none;
  color: var(--c60);
}
:is(.add-applies-to, .remove-applies-to):hover i {
  pointer-events: none;
  color: var(--fg);
}
/************ help popup ************/
#help-popup {
  --pad-x: 1.5rem;
  --pad-y: 1rem;
  --pad-y2: calc(var(--pad-y) / 1.5);
  --top: 3rem;
  top: var(--top);
  right: 3rem;
  max-width: calc(100% - var(--header-width) - 6rem);
  position: fixed;
  background-color: var(--bg);
  box-shadow: 3px 3px 30px rgba(0, 0, 0, 0.5);
  z-index: 99;
  &:is(.big, .wide) {
    max-width: 100%;
  }
  &.big {
    box-shadow: rgba(0, 0, 0, 0.45) 0px 0px 0px 100000px !important;
    .normal-layout & {
      left: calc(var(--header-width) - 3rem);
    }
    .CodeMirror {
      min-height: 2rem;
      height: 70vh;
    }
  }
  &.danger {
    .title {
      background-color: firebrick;
      color: white;
    }
    > .i-close {
      color: white;
    }
  }
  .title {
    font-weight: bold;
    background-color: rgba(128, 128, 128, .15);
    padding: var(--pad-y2) var(--pad-x);
  }
  .contents {
    max-height: calc(var(--100dvh) - var(--top) - 4rem);
    overflow-y: auto;
    padding: var(--pad-y) var(--pad-x);
  }
  > .i-close {
    position: absolute;
    right: 0;
    top: 0;
    padding: var(--pad-y2) .5em;
    font-size: 14px;
  }
  :is(input[type="search"], .CodeMirror) {
    margin: 3px;
  }
  .keymap-list input[type="search"] {
    margin: 0 0 2px;
  }
  .keymap-list {
    font-size: var(--font-size);
    padding: 0 3px 0 0;
    border-spacing: 0;
    word-break: break-all;
    input {
      width: 100%;
    }
    tr:nth-child(odd) {
      background-color: rgba(0, 0, 0, 0.07);
    }
    td:first-child {
      white-space: nowrap;
      font-family: monospace;
      padding-right: 0.5rem;
    }
  }
  .buttons {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: var(--pad-y2) 0 calc(var(--pad-y2) - var(--pad-y)) 0;
    > :nth-child(n + 2) {
      margin: 0 0 0 .75rem;
    }
  }
  .non-windows & .buttons {
    direction: rtl;
    justify-content: flex-start;
    > :nth-child(n + 2) {
      margin: 0 .5em 0 0;
    }
  }
  button[name^="import"] {
    line-height: 1.5rem;
    pointer-events: none;
    opacity: 0.5;
  }
  &.ready button[name^="import"] {
    pointer-events: all;
    opacity: 1.0;
  }
  .rules {
    padding: 0 15px;
    li {
      padding-top: .5em;
    }
    p {
      margin: .25em 0;
    }
  }
}

/************ lint ************/
#lint {
  overflow-x: hidden;
  > summary h2 {
    display: inline-flex;
  }
  table {
    border-spacing: 0;
    line-height: 1.0;
    width: 100%;
    font-size: 85%;
    cursor: pointer;
  }
  .report {
    display: flex;
  }
  .empty {
    display: none;
  }
  .caption {
    vertical-align: top;
    line-height: 16px;
    font-weight: bold;
  }
  .report:not(.empty) ~ :not(.empty) {
    border-top: 1px dotted rgba(128, 128, 128, .5);
    margin-top: .25em;
    padding-top: .25em;
  }
  tr:hover {
    background-color: hsla(180, 50%, 36%, .2);
  }
  td {
    padding: 0;
    line-height: 16px;
    &:last-child {
      width: 100%;
    }
    &[role="line"] {
      padding-left: 0.25rem;
    }
    &[role="severity"] {
      font-size: 0;
      width: 16px;
    }
    &:is([role="line"], [role="sep"]) {
      text-align: right;
    }
    &[role="col"] {
      text-align: left;
      padding-right: 0.25rem;
    }
    &[role="message"] {
      text-align: left;
      white-space: nowrap;
    }
  }
}
#issue-count::before {
  content: ':\A0';
}
#message-box.center.lint-config #message-box-contents {
  text-align: left;
}
#help-popup .cm-active-linter-rule {
  font-weight: bold;
  text-decoration: underline;
  background-color: rgba(128, 128, 128, .2);
}

/************ CSS beautifier ************/
.beautify-options {
  white-space: nowrap;
  div {
    float: left;
    display: flex;
    align-items: center;
    &[newline="true"] + div {
      clear: left;
      span[indent] {
        padding-left: 2rem;
      }
    }
  }
  > label {
    top: 1em;
    margin: 1ex 0;
  }
  &::after {
    clear: both;
    display: block;
    content: "";
    height: 1rem;
  }
  span {
    font-weight: bold;
    font-family: monospace;
  }
  select {
    border: none;
    background: var(--c95);
    font-family: monospace;
    font-weight: bold;
    padding-left: 4px;
    margin-left: 4px;
  }
}
.beautify-hint {
  width: 0;
  min-width: 100%;
  font-size: 90%;
}

/************ single editor **************/
.usercss .CodeMirror-focused {
  box-shadow: none;
}

.sectioned .usercss-only,
.usercss .sectioned-only {
  display: none !important; /* hide during page init */
}

.single-editor {
  position: relative;
  height: 100%;
  .CodeMirror {
  /* WARNING! If you change this or .single-editor make sure editor opens huge styles like GitHub Dark instantly */
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: auto;
    border: none;
    outline: none;
  }
}

#footer a {
  color: var(--c20);
  transition: color .5s;
  &:hover {
    color: var(--c40);
  }
}

/************ line widget *************/
.CodeMirror-linewidget {
  .applies-to {
    margin: 0 0 .25em;
    padding: var(--target-gapY) calc(.25rem + var(--cm-bar-width, 0)) 0 .75rem;
    font-family: var(--family);
  }
  .applies-to-everything {
    margin-left: 0.2rem;
  }
}

/************ full-width only ************/
/* TODO: maybe move more rules here so we don't need to reset them in @media(max-width: 850px) */
@media (min-width: 851px) {
  #header > :not(#details-wrapper):not(#header-resizer) {
    margin-left: var(--pad);
    margin-right: var(--pad);
  }
  #publish[open],
  #details-wrapper details:not([open]) {
    flex: 0 0 auto;
  }
  #details-wrapper details {
    &[open]:not(:last-child) {
      margin-bottom: var(--pad05);
    }
    &:last-child {
      padding-bottom: var(--pad);
    }
  }
}

/************ reponsive layouts ************/
@media screen and (max-width: 850px), compact {
  .usercss body {
    display: flex;
    flex-direction: column;
  }
  .usercss #header,
  #header:not(.sticky) {
    position: static;
  }
  #header {
    flex: 0 0 auto;
    height: unset;
    width: 100%;
    overflow: visible;
    border-right: none;
    border-bottom: 1px dashed var(--c65);
    padding: var(--pad05) var(--pad05) 0;
    &:not(.sticky) {
      flex-direction: row;
      flex-wrap: wrap;
    }
    &.sticky {
      flex-direction: row;
      box-shadow: 0 0 3rem -.75rem;
      #basic-info {
        margin: 0;
      }
      #preview-errors {
        position: absolute;
        top: var(--pad05);
        right: var(--pad05);
        margin: .25em 0 0 !important;
      }
    }
    details > :not(summary) {
      margin-left: 0;
    }
    h2 {
      font-size: 14px;
    }
    .settings h2 {
      max-width: 10vw;
    }
  }
  #header.sticky .hide-if-sticky,
  #heading,
  h2 {
    display: none;
  }
  .popup-window {
    #header.sticky #preview-errors {
      right: calc(var(--pad05) + 24px);
    }
    #details-wrapper {
      margin-right: var(--popup-button-width);
    }
  }
  #basic-info {
    margin: 0 var(--popup-button-width) var(--pad05) 0;
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    #header:not(.sticky) & {
      width: 100%;
    }
    #name,
    > *:first-child {
      flex-grow: 1;
    }
    > *:not(:last-child) {
      margin-right: var(--pad05);
    }
    > a {
      order: 1;
      align-self: center;
    }
  }
  #actions .buttons {
    flex-wrap: nowrap;
  }
  #details-wrapper {
    display: inline-flex;
    vertical-align: top;
    max-width: 100%;
    flex-direction: row;
    margin: .25em .25em var(--pad05);
    > details[open] {
      margin-top: 0;
      z-index: 50;
      &:is(:hover, :focus-within) {
        z-index: 51;
      }
      > summary {
        position: static;
        margin-top: 0;
        + * {
          position: absolute;
          overflow: hidden auto;
          max-height: var(--max-height, 25vh);
          max-width: calc(100vw - 2 * var(--pad));
          background: var(--bg);
          box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
          padding: var(--pad);
        }
      }
    }
  }
  #options > main {
    display: flex;
    flex-wrap: wrap;
    > :first-child {
      margin: 0 1em 0 0;
    }
    > :nth-child(n + 2) {
      margin: 0;
    }
  }
  @media (max-height: 500px) {
    #details-wrapper {
      --max-height: 50px;
    }
  }
  #toc,
  .lint-report-container {
    max-width: 50vw;
  }
  #publish[open],
  #lint:not([open]) {
    flex: 0 0 auto;
    overflow-x: hidden;
  }
  #publish dd {
    white-space: normal;
    max-height: 1.25em;
  }
  #sections {
    padding: 0;
  }
  .section {
    padding: var(--pad05);
  }
  .single-editor {
    overflow: hidden;
    flex: 1;
  }
  #help-popup {
    &, &.big {
      width: max-content;
      height: min-content;
      max-width: calc(100vw - 2rem);
      --top: 7rem;
      right: 1rem;
      margin: auto;
    }
    &[style*="left:"] {
      left: 1rem !important;
      margin-top: -2.35rem;
    }
    :is(.CodeMirror-lint-markers, .CodeMirror-guttermarker-subtle) {
      display: none !important;
    }
    .CodeMirror-linenumber {
      padding: 0;
    }
  }
}
